//
//  ZYMainTabbar.swift
//  weibo007
//
//  Created by 陈宗毅 on 15/12/15.
//  Copyright © 2015年 czy. All rights reserved.
//

import UIKit

class ZYMainTabbar: UITabBar {
    
    ///准备一个闭包
    var composeBtnClick:(() -> ())?
    
    
    
    private let count = 5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算uitabBarButton的宽度
        let width = self.bounds.width / CGFloat(count)
        
        //最左边的frame
        let frame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
        //遍历所有子控件
        var index = 0
        
        for view in self.subviews
        {
            if view is UIControl && !(view is UIButton)
            {
                view.frame = CGRectOffset(frame, CGFloat(index) * width, 0)
                index += index==1 ? 2:1
                
            }
        }
        
        composeButton.frame = CGRectOffset(frame, 2.0 * width, 0)
    }
    
    ///加号按钮的点击事件
    func composeButtonClick()
    {
//        print("composeBtnClick")
        composeBtnClick?()
    }
    
    //设置加号按钮的frame
    
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到父控件上面
        //在代码块不能省略self
        self.addSubview(button)
        
        return button
        }()
}
